# -*- coding:utf-8 -*-

from django.core import serializers as SZ
from django.contrib.auth.models import User,Group
from rest_framework import serializers
from .models import Projects
from .models import Tasks


class ProjectsSerializers(serializers.ModelSerializer):
    tasks = serializers.SerializerMethodField()
    group_users = serializers.SerializerMethodField()
    class Meta:
        model = Projects
        fields = ('id','title','tasks','description','start_time','end_time', 'status','user_group','create_user', 'group_users')

    def get_tasks(self, obj):
        tasks = Tasks.objects.filter(project_id = obj.id)
        return tasks

    def get_group_users(self,obj):
        users = [
            {'username':'s'},
            {'username':'s'}
        ]
        group = Group.objects.filter(id= obj.user_group_id).first()
        users = group.user_set.all()
        group_users = []
        for u in users:
            user = {}
            user['username']= u.username
            group_users.append(user)
        return group_users
